﻿namespace Arms.Application.Modules.DiningRoom
{
    using Arms.Application.Modules.DiningRoom.DTOs;
    using Arms.Application.Services;
    using Arms.Domain;
    using Arms.Domain.Modules.DiningRoom;
    using Arms.Domain.Modules.Orders;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class DiningRoomQueryService : ServiceBase, IDiningRoomQueryService
    {
        public DiningRoomQueryService(IUnitOfWork uow) : base(uow) { }

        public DiningRoomLayoutDTO FindLayout(int diningRoomLayoutID)
        {
            DiningRoomLayoutDTO layoutDTO = new DiningRoomLayoutDTO();
            var layout = base.UnitOfWork.Repository<DiningRoomLayout>().Find(diningRoomLayoutID);
            var tablesWithOrders = base.UnitOfWork.Repository<OrderHeader>()
                                                  .GetAll(x => x.DiningRoomTableID.HasValue).ToList(); 
            //&&
            //                                                   x.OrderState == OrderState.Open &&
            //                                                   x.ActiveState == EntityActiveState.Active);

            if (layout != null)
            {
                layoutDTO.ID = layout.ID;
                layoutDTO.Name = layout.Name;
                layoutDTO.Description = layout.Description;
                layoutDTO.Comment = layout.Comment;

                foreach (var floor in layout.Floors)
                {
                    DiningRoomFloorDTO floorDTO = new DiningRoomFloorDTO();
                    floorDTO.ID = floor.ID;
                    floorDTO.Order = floor.Order;

                    foreach (var table in floor.Tables)
                    {
                        DiningRoomTableDTO tableDTO = new DiningRoomTableDTO();
                        tableDTO.ID = table.ID;
                        tableDTO.TableNumber = table.TableNumber;
                        tableDTO.NumberOfSeats = table.NumberOfSeats;

                        if (tablesWithOrders.Select(x => x.DiningRoomTableID.Value).Contains(table.ID))
                        {
                            tableDTO.OrderHeaderID = tablesWithOrders.SingleOrDefault(x => x.DiningRoomTableID.Value == table.ID).ID;
                        }

                        floorDTO.Tables.Add(tableDTO);
                    }

                    layoutDTO.Floors.Add(floorDTO);
                }
            }

            return layoutDTO;
        }

        public DiningRoomTableDTO FindTable(int diningRoomTableID)
        {
            DiningRoomTableDTO dto = new DiningRoomTableDTO();

            var table = base.UnitOfWork.Repository<DiningRoomTable>().Find(diningRoomTableID);
            var orderheader = base.UnitOfWork.Repository<OrderHeader>().Single(x => x.DiningRoomTableID == diningRoomTableID);

            if (table != null)
            {
                dto.TableNumber = table.TableNumber;
                dto.ID = table.ID;
                dto.NumberOfSeats = table.NumberOfSeats;

                if (orderheader != null)
                {
                    dto.OrderHeaderID = orderheader.ID;
                }
            }

            return dto;
        }
    }
}
